package top.ashman.demo.springcloud.feign.client.hystrix;

import feign.hystrix.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author sunzhaojie
 * @date 2018-12-12
 */
@FeignClient(name = "hystrix", path = "/my", fallbackFactory = HystrixFeignClient.HystrixFeignClientFallbackFactory.class)
public interface HystrixFeignClient {

    /**
     * Sleep and Hello with Milliseconds
     * @param milliseconds Milliseconds
     * @return Hello World
     * @throws InterruptedException InterruptedException
     */
    @GetMapping("/sleep/{milliseconds}")
    String sleepAndHello(@PathVariable Long milliseconds) throws InterruptedException;

    /**
     * Hello Name
     * @param name Name
     * @return Hello Name
     */
    @GetMapping("/hello/{name}")
    String hello(@PathVariable String name);

    @Component
    class HystrixFeignClientFallbackFactory implements FallbackFactory<HystrixFeignClient> {

        @Override
        public HystrixFeignClient create(Throwable throwable) {
            return new HystrixFeignClient() {
                @Override
                public String sleepAndHello(Long milliseconds) {
                    return "Sleep and Hello Fallback";
                }

                @Override
                public String hello(String name) {
                    return "Hello Name Fallback";
                }
            };
        }
    }
}

